package com.zhou.dto;

import lombok.Data;

/**
 * 统一返回结果集实体类
 * @param <T> 返回数据对象
 */
@Data
public class Result<T> {

    /**
     * 错误码
     */
    private Integer resultCode;

    /**
     * 响应信息，一般为前端提示信息
     */
    private String Msg;

    /**
     * 返回值，一般为成功后返回的数据
     */
    private T data;

    /**
     * 错误详情，一般为失败后的详细原因，如空指针之类的
     */
    private String errorDetail;

    private String error;
    private Integer status;

    public Result() {}

    /**
     * 发生错误时，只需要填写结果码和结果消息，无需返回具体数据
     * @param resultCode
     * @param Msg
     */
    public Result(Integer resultCode, String Msg,String error) {
        this.resultCode = resultCode;
        this.Msg = Msg;
        this.error = error;
    }

    /**
     * 没有错误时，需要携带结果码、结果消息、具体数据返回给前端
     * @param resultCode
     * @param Msg
     * @param data
     */
    public Result(Integer resultCode, String Msg, T data) {
        this.resultCode = resultCode;
        this.Msg = Msg;
        this.data = data;
        this.error = "";
        this.status=1;
    }

    /**
     * 配合静态对象直接设置 data 参数
     * @param data 需要返回的数据
     * @return
     */
    public Result setNewData(T data) {
        Result result = new Result();
        result.setResultCode(this.resultCode);
        result.setMsg(this.Msg);
        result.setError("");
        result.setStatus(1);
        result.setErrorDetail(this.errorDetail);
        result.setData(data);
        return result;
    }

    /**
     * 配合静态对象直接设置 errorMsg 参数
     * @param Msg
     * @return
     */
    public Result setNewResult(String Msg,T data) {
        Result result = new Result();
        result.setResultCode(this.resultCode);
        result.setMsg(Msg);
        result.setError("");
        result.setStatus(1);
        result.setErrorDetail(this.errorDetail);
        result.setData(data);
        return result;
    }

    /**
     * 配合静态对象直接设置 errorMsg 参数
     * @param error
     * @return
     */
    public Result setErrorResult(String error) {
        Result result = new Result();
        result.setResultCode(this.resultCode);
        result.setMsg(error);
        result.setError(error);
        result.setErrorDetail(this.errorDetail);
        if(this.resultCode ==202||this.resultCode ==200){
            result.setStatus(0);
        }else {
            result.setStatus(1);
        }
        return result;
    }

    public static final Result SUCCESS = new Result(200, "成功","");

    public static final Result INSERT_SUCCESS = new Result(200, "新增成功","");

    public static final Result UPDATE_SUCCESS = new Result(200, "更新成功","");

    public static final Result DELETE_SUCCESS = new Result(200, "删除成功","");

    public static final Result UPLOAD_SUCCESS = new Result(200, "上传成功","");

    public static final Result DOWNLOAD_SUCCESS = new Result(200, "下载成功","");

    public static final Result LOGIN_SUCCESS = new Result(200, "登陆成功","");

    public static final Result LOGOUT_SUCCESS = new Result(200, "登出成功","");

    public static final Result LOGIN_ERROR = new Result(201, "登陆错误","登陆错误");

    public static final Result LOGIN_EXPIRE = new Result(202, "登陆过期","登陆过期");

    public static final Result ACCESS_LIMITED = new Result(301, "访问受限","访问受限");

    public static final Result ARGS_ERROR = new Result(501, "参数错误","参数错误");

    public static final Result UNKOWN_ERROR = new Result(502, "系统异常","系统异常");

    public static final Result INSERT_ERROR = new Result(503, "新增错误","新增错误");

    public static final Result UPDATE_ERROR = new Result(504, "更新错误","更新错误");

    public static final Result DELETE_ERROR = new Result(506, "删除错误","删除错误");

    public static final Result UPLOAD_ERROR = new Result(507, "上传错误","上传错误");

    public static final Result DOWNLOAD_ERROR = new Result(508, "下载错误","下载错误");

    public static final Result OTHER_SYSTEM_ERROR = new Result(509, "调用系统异常","调用系统异常");

}
